﻿
// Condition: 15. Modify your last program nad try to make it work for any type, not just integer( decimal,
//                float, byte, etc.). Use generic method.

using System;
using System.Collections.Generic;



class Multitask
{

    static void Sum<T>(T[] array)
    {
        dynamic sum = 0;
        for (int i = 0; i < array.Length; i++)
        {
            sum += array[i];
        }

        Console.WriteLine("The sum of given integers is: {0}", sum);
    }

    static void Product<T>(T[] array)
    {
        dynamic product = 1;
        for (int i = 0; i < array.Length; i++)
        {
            product *= array[i];
        }

        Console.WriteLine("The product of given integers is: {0}", product);
    }

    static void Max<T>(T[] array)
    { 
        // define two new variables
        dynamic p;
        dynamic q;

        for (int i = 0; i < array.Length - 1; i++)
        {
            p = array[i];

            for (int j = i + 1; j < array.Length; j++)
            {
                q = array[j];
                if (p<q)
                {
                    p=p+q;
                    q=p-q;
                    p=p-q;
                    array[i]=p;
                    array[j]=q;
                }
            }
        }

        Console.WriteLine("The maximum of given integers is: {0}", array[0]);
    }

    static void Min<T>(T[] array)
    {
        // define two new variables
        dynamic p;
        dynamic q;

        for (int i = 0; i < array.Length - 1; i++)
        {
            p = array[i];

            for (int j = i + 1; j < array.Length; j++)
            {
                q = array[j];
                if (p < q)
                {
                    p = p + q;
                    q = p - q;
                    p = p - q;
                    array[i] = p;
                    array[j] = q;
                }
            }
        }

        Console.WriteLine("The minimum of given integers is: {0}", array[array.Length - 1]);
    }

    static void Main(string[] args)
    {
        // input numbers
        Console.Write("Enter number of arguments: ");
        int argument = int.Parse(Console.ReadLine());
        // you can choose different type
        double[] argArray = new double[argument];


        for (int i = 0; i < argument; i++)
        {
            Console.Write("Number[{0}]= ", i + 1);
            argArray[i] = double.Parse(Console.ReadLine());
        }

        // create menu to choose different methods
        Console.WriteLine("Menu: ");
        Console.WriteLine("Finding the minimum of numbers press 1.");
        Console.WriteLine("Finding the maximum of numbers press 2.");
        Console.WriteLine("Calculate the sum of numbers press 3");
        Console.WriteLine("Calculate the product of numbers press 4");
        int menu = int.Parse(Console.ReadLine());
        if (menu == 1)
        {
            Min(argArray);
        }
        else if (menu == 2)
        {
            Max(argArray);
        }
        else if (menu == 3)
        {
            Sum(argArray);
        }
        else if (menu == 4)
        {
            Product(argArray);
        }

    }
}

